/*
 * Created by JFormDesigner on Fri Nov 01 22:03:27 CST 2024
 */

package com.hafu.view.GYS;

import com.hafu.model.Gysinfo;
import com.hafu.service.GysinfoService;
import com.hafu.util.StringUtil;
import com.mysql.jdbc.StringUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;

/**
 * @author admin
 */
public class ManageGYSfrm extends JFrame {
    private GysinfoService gysinfoService = new GysinfoService();
    public ManageGYSfrm() {
        initComponents();
        fillTable();
    }

    private void fillTable() {
        ArrayList<Gysinfo> gysinfoList = gysinfoService.getGysInfoByName(new Gysinfo());

        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        for (Gysinfo gysinfo : gysinfoList){
            Vector<Object> rowData = new Vector<>();
            rowData.add(gysinfo.getId());
            rowData.add(gysinfo.getName());
            rowData.add(gysinfo.getTel());

            model.addRow(rowData);
        }
    }

    private  void button1(ActionEvent e) {
        // TODO add your code here 查询供应商信息
        String name = textField1.getText();
        Gysinfo gysInfo = new Gysinfo();
        gysInfo.setName(name);
        ArrayList<Gysinfo> gysInfoList = gysinfoService.getGysInfoByName(gysInfo);
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        if (gysInfoList.size()>0){
            for (Gysinfo gysinfo : gysInfoList){
                Vector<Object> rowData = new Vector<>();
                rowData.add(gysinfo.getId());
                rowData.add(gysinfo.getName());
                rowData.add(gysinfo.getTel());

                model.addRow(rowData);
            }

        }else {
            JOptionPane.showMessageDialog(null, "没有查询到供应商信息");

        }


    }



            private void table1MouseClicked(MouseEvent e) {
                // TODO add your code here
                int row = table1.getSelectedRow();
                DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
               int chooseId = (int) dtn.getValueAt(row, 0);
                String name = (String) dtn.getValueAt(row, 1);
                String tel = (String) dtn.getValueAt(row, 2);
                textField2.setText(name);
                textField3.setText(tel);

            }

            private void button2(ActionEvent e) {
                // TODO add your code here
                String name = textField2.getText();
                if (StringUtils.isNullOrEmpty(name)){
                    JOptionPane.showMessageDialog(null, "供应商名称不能为空");
                    return;
                }
                String tel = textField3.getText();
                if (StringUtil.isEmpty(tel)){
                    JOptionPane.showMessageDialog(null, "供应商电话不能为空");
                    return;
                }
                int row = table1.getSelectedRow();
                DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
                int chooseId = (int) dtn.getValueAt(row, 0);
                Gysinfo gysInfo = new Gysinfo(name, tel);
                Boolean aBoolean = gysinfoService.updateGysInfo(gysInfo);
                if (aBoolean){
                    JOptionPane.showMessageDialog(null, "修改成功");
                    this.dispose();
                }else {
                    JOptionPane.showMessageDialog(null, "修改失败");
                }
    }

    private void button3(ActionEvent e) {
        // TODO add your code here 删除供应商
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId=(int) dtn.getValueAt(row, 0);
        Boolean isDeleteGysInfo = gysinfoService .deleteGysInfo(chooseId);
        if(isDeleteGysInfo){
            JOptionPane.showMessageDialog(null, "删除成功");
            fillTable();
        }else {
            JOptionPane.showMessageDialog(null, "删除失败");
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u4f9b\u5e94\u5546");
        Container contentPane = getContentPane();


        //---- label1 ----
        label1.setText("\u4f9b\u5e94\u5546\u540d\u5b57");

        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));
        {
            table1.setModel(new DefaultTableModel(
                    new Object[][]{
                            {"", null, null},
                            {null, null, null},
                    },
                    new String[]{
                            "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                    }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }
        label2.setText("\u4f9b\u5e94\u5546\u540d\u5b57");
        label3.setText("\u4f9b\u5e94\u5546\u7535\u8bdd");
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));
        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                .addContainerGap(106, Short.MAX_VALUE)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 556, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(201, Short.MAX_VALUE)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                        .addGap(41, 41, 41)
                                        .addGroup(contentPaneLayout.createParallelGroup()
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                                                        .addGap(41, 41, 41)
                                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
                                                        .addGap(185, 185, 185)
                                                        .addComponent(button1))
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                        .addGroup(contentPaneLayout.createParallelGroup()
                                                                .addComponent(label3)
                                                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))
                                                        .addGap(35, 35, 35)
                                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false))
                                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 146, Short.MAX_VALUE)
                                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 146, Short.MAX_VALUE))
                                                .addGap(156, 156, 156)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(button3)
                                                        .addComponent(button2))))
                                .addContainerGap(265, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(button1)
                                        .addComponent(label1)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(button2)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(33, 33, 33)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label3)
                                        .addComponent(button3)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(62, 62, 62))

        );

        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
}











